package com.hangzhou.xyj.db.preferences;

import android.content.Context;

import com.hangzhou.xyj.utils.PublicUtil;
import com.hangzhou.xyj.utils.constants.Constants;

import java.util.Date;

/**
 * @author neo.duan
 * @ClassName: CommonPreferences
 * @Description: 设置界面的配置文件
 */
public class CommonPreferences extends BasePreferences {
    // 文件名
    private static final String PREFERENCES_NAME = Constants.PROJECT;

    private static final String KEY_USER_ID = "userId";
    private static final String KEY_UID = "uid";
    private static final String KEY_ACCESS_TOKEN = "access_token";
    private static final String KEY_EXPIRES_IN = "expires_in";
    private static final String KEY_LOGIN_TYPE = "login_type";
    private static final String KEY_PUSH = "push";
    private static final String KEY_TAG = "tag";
    private static final String KEY_GUIDE = "guide";
    private static final String KEY_LAST_CHECK_UPDATE = "last_check_update"; //上一次检查更新的时间


    /**
     * 保存数据
     *
     * @param key
     * @return boolean true:成功 false:失败
     */
    public static boolean saveData(Context context, String key, Object value) {
        return saveData(context, PREFERENCES_NAME, key, value);
    }

    /**
     * 从文件中获取数据
     *
     * @param key
     * @param defValue
     * @return
     */
    public static Object getData(Context context, String key, Object defValue) {
        if (context == null) {
            return defValue;
        }
        return getData(context, PREFERENCES_NAME, key, defValue);
    }

    /**
     * 移除指定KEY
     *
     * @param key
     */
    public static void removeKey(Context context, String key) {
        remove(context, PREFERENCES_NAME, key);
    }

    /**
     * 清除所有数据
     */
    public static void clear(Context context) {
        clear(context, PREFERENCES_NAME);
    }

    /**
     * @param uid
     */
    public static void saveUserId(Context context, String uid) {
        saveData(context, KEY_USER_ID, uid);
    }

    /**
     * 不要直接调这里，调Global.getUserId();
     *
     * @return
     */
    public static String getUserId(Context context) {
        return (String) getData(context, KEY_USER_ID, "-1");
    }

    /**
     * 设置是否禁用消息
     *
     * @param userId
     * @param enabled
     */
    public static void setEnablePush(Context context, String userId, boolean enabled) {
        saveData(context, userId + "_" + KEY_PUSH, enabled);
    }

    /**
     * 获取是否禁用消息, true消息可以，false 消息被禁用
     *
     * @param userId
     * @return
     */
    public static boolean getEnablePush(Context context, String userId) {
        boolean enabled = (Boolean) getData(context, userId + "_" + KEY_PUSH, true);
        return enabled;
    }


    /**
     * 清除Token信息
     */
    public static void clearAccessToken(Context context) {
        removeKey(context, KEY_UID);
        removeKey(context, KEY_ACCESS_TOKEN);
        removeKey(context, KEY_EXPIRES_IN);
        removeKey(context, KEY_LOGIN_TYPE);
    }

    public static void saveValue(Context context, String key, boolean value) {
        saveData(context, key, value);
    }

    public static void saveValue(Context context, String key, int value) {
        saveData(context, key, value);
    }

    public static void saveValue(Context context, String key, String value) {
        saveData(context, key, value);
    }

    public static boolean getValue(Context context, String key, boolean defValue) {
        return (Boolean) getData(context, key, defValue);
    }

    public static int getValue(Context context, String key, int defValue) {
        return (Integer) getData(context, key, defValue);
    }

    public static String getValue(Context context, String key, String defValue) {
        return (String) getData(context, key, defValue);
    }

    public static boolean isShowGuideView(Context context) {
        int lastVersion = (int) getData(context, KEY_GUIDE, -1);
        int currentVersion = PublicUtil.getCurrentAppVersion(context);
        return lastVersion == currentVersion;
    }

    public static void setShowGuideView(Context context, boolean isShow) {
        saveData(context, KEY_GUIDE, isShow ? PublicUtil.getCurrentAppVersion(context) : -1);
    }

    /**
     * 获取上次检查更新的时间
     *
     * @param context
     * @return
     */
    public static Date getLastAppCheckUpdate(Context context) {
        long lastTimeMillis = (long) getData(context, KEY_LAST_CHECK_UPDATE, 0L);
        return new Date(lastTimeMillis);
    }

    /**
     * 保存上次检查更新的时间
     *
     * @param context
     * @param timeMillis
     */
    public static void saveLastAppCheckUpdate(Context context, long timeMillis) {
        saveData(context, KEY_LAST_CHECK_UPDATE, timeMillis);
    }

}
